
module.exports = {
    init: function (t, e) {
        this.fileSystemManager = wx.getFileSystemManager(), this.rootPath = "" + wx.env.USER_DATA_PATH,
            this.urlRoot = t, this.gameName = e, console.log(this.gameName);
    },
    getFileName: function (t) {
        for (var e = t.substring(t.indexOf(this.urlRoot), t.length); - 1 != e.indexOf("/");) {
            var n = e.indexOf("/");
            e = e.substring(0, n) + "-" + e.substring(n + 1, e.length);
        }
        return e;
    },
    getFile: function (t) {
        var e = this.getFileName(t.url),
            n = this.rootPath + "/" + e,
            o = this;
        this.fileSystemManager.stat({
            path: n,
            success: function (o) {
                o.stats.isDirectory(), o.stats.isFile() && (console.log("这是文件： " + e), t.success(n));
            },
            fail: function (i) {
                console.log(e + "文件不存在，下载"), console.log(t.url + "?" + o.gameName), wx.downloadFile({
                    url: t.url + "?" + o.gameName,
                    success: function (e) {
                        console.log("下载成功"), wx.saveFile({
                            tempFilePath: e.tempFilePath,
                            filePath: n,
                            success: function (e) {
                                console.log("保存到本地"), t.success(n);
                            },
                            fail: function (e) {
                                console.log(e), t.fail();
                            }
                        });
                    },
                    fail: function (e) {
                        console.log(e), t.fail();
                    }
                });
            }
        });
    },
    removeFile: function (t) {
        var e = this.getFileName(t.url),
            n = this.rootPath + "/" + e,
            o = this;
        this.fileSystemManager.stat({
            path: n,
            success: function (e) {
                o.fileSystemManager.unlink({
                    filePath: n,
                    success: function () {
                        t.success();
                    },
                    fail: function (t) {
                        console.log(t);
                    }
                });
            },
            fail: function (e) {
                console.log("本地不存在"), t.success();
            }
        });
    },
    showFile: function () {
        this.fileSystemManager.readdir({
            dirPath: "" + wx.env.USER_DATA_PATH,
            success: function (t) {
                console.log(t);
            },
            fail: function () {}
        });
    }
}